package com.cvai.controller;


import com.cvai.R.Result;
import com.cvai.aiservice.ChatAssistant;
import com.cvai.domain.dto.ChatDto;
import com.cvai.domain.po.Chat;
import com.cvai.service.IChatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import reactor.core.publisher.Flux;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cv
 * @since 2025-03-09
 */
@Slf4j
@RestController
@RequestMapping("/chat")
@CrossOrigin
@RequiredArgsConstructor
public class ChatController {
    private final IChatService chatService;
    //生成
    @PostMapping(value = "/{memoryId}", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> chatByFlux(@PathVariable("memoryId") String memoryId, @RequestBody ChatDto chatDto) {
        if(chatDto == null)
            throw new RuntimeException("chatDto不能为空");
        if(!memoryId.equals(chatDto.getMemoryId()))
            throw new RuntimeException("memoryId和memoryId不一致");
        return chatService.chat(chatDto);
    }

    @GetMapping("/{memoryId}")
    public Result<List<Chat>> list(@PathVariable("memoryId") String memoryId){
        return Result.success(chatService.listByMemoryId(memoryId));
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable Long id,String name){
        chatService.update().eq("id",id).set("chat_name",name).update();
        return Result.success();
    }
    @PutMapping("/del/{id}")
    public Result del(@PathVariable Long id){
        chatService.update().eq("id",id).set("is_del",1).update();
        return Result.success();
    }
}
